package com.zxy.controller;

import com.zxy.annotation.RequestMapping;
import com.zxy.annotation.RequestMappingHandler;
import com.zxy.utils.Constants;
import com.zxy.utils.OKHttpUtil;
import org.apache.commons.io.IOUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Objects;

/**
 * @author zxy
 * @date 2019-04-28 16:38
 */
@WebServlet("/gaode/*")
public class GaodeController extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        String requestURI = request.getRequestURI();
        String action = requestURI.substring(request.getServletContext().getContextPath().length() + 1);
        action = action.substring(action.indexOf("/"));
        RequestMappingHandler.handle(this.getClass(), this, action, request, response);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        doPost(req, resp);
    }

    @RequestMapping("/staticMap")
    public void staticMap(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        //response.setContentType("text/html;charset=utf-8");
        OutputStream outputStream = response.getOutputStream();
        String lonlat = request.getParameter("location");
        InputStream is = (InputStream) OKHttpUtil.get("https://restapi.amap.com/v3/staticmap?markers=mid,0xFF0000,A:"+lonlat+"&key=a4094c7fab084436d316a6e60d525b00", Constants.REQUEST_STREAM);
        IOUtils.copy(Objects.requireNonNull(is),outputStream);
    }

}
